﻿using System;
using System.Collections.Generic;
using System.IO;

namespace DemoRealtimeData.Service
{
    public sealed class Config
    {
        private static Config _default;

        private readonly string[] _devices;
        private readonly float _interval;//unit: ms
        private readonly int _total;

        public Config(string fileName)
        {
            IDictionary<string, string> pairs = Load(fileName);
            _devices = pairs["devices"].Split(',');
            _interval = float.Parse(pairs["interval"]);
            _total = int.Parse(pairs["total"]);
        }

        public string[] Devices { get { return _devices; } }
        public float Interval { get { return _interval; } }
        public int Total { get { return _total; } }

        private static IDictionary<string, string> Load(string fileName)
        {
            var dicts = new Dictionary<string, string>();
            using (var reader = File.OpenText(fileName))
            {
                while (!reader.EndOfStream)
                {
                    var segments = reader.ReadLine().Split('=');
                    dicts.Add(segments[0], segments[1]);
                }//while               
            }//using
            return dicts;
        }

        public static Config Default
        {
            get
            {
                if (_default == null)
                {
                    _default = new Config("config.txt");
                }
                return _default;
            }
        }
    }// Config
}
